package com.yxplearn.mybatisplus.mybatisplus_simple.type_handler;

import java.util.HashMap;
import java.util.Map;

public class MapTypeHandler extends AbstractCollectionTypeHandler<Map<Object, Object>> {

    @Override
    public Map<Object, Object> parseToCollection(String value) {
        Map<Object, Object> mapResult = new HashMap<>();
        String[] split = value.split(";");
        for (int i = 0; i < split.length; i++) {
            String[] split1 = split[i].split(":");
            mapResult.put(split1[0], split1[1]);
        }
        return mapResult;
    }

    @Override
    public String parseToString(Map<Object, Object> map) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<Object, Object> set : map.entrySet()) {
            stringBuilder.append(set.getKey().toString()).append(":").append(set.getValue().toString()).append(";");
        }
        return stringBuilder.toString();
    }
}
